﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Resources;
using System.Web;
using System.Web.Mvc;
using System.Resources;
using System.Collections;

namespace InnoWeb.CEND.UI.MVC.Areas.Admin.Controllers
{
    public class LanguageController : Controller
    {
        public ActionResult Index()
        {
            List<string> resxList = new List<string>();
            string resourcespath = Request.PhysicalApplicationPath + "App_GlobalResources";
            DirectoryInfo dirInfo = new DirectoryInfo(resourcespath);
            foreach (FileInfo filInfo in dirInfo.GetFiles())
            {
                string filename = filInfo.Name;
                resxList.Add(filename);
            }
            //resxList.Insert(0, "Select a Resource File");

            SelectList dropList = new SelectList(resxList);

            return View(dropList);
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            var x = collection["selection"];

            //List<string> resxList = new List<string>();
            //string resourcespath = Request.PhysicalApplicationPath + "App_GlobalResources";
            //DirectoryInfo dirInfo = new DirectoryInfo(resourcespath);
            //foreach (FileInfo filInfo in dirInfo.GetFiles())
            //{
            //    string filename = filInfo.Name;
            //    resxList.Add(filename);
            //}
            ////resxList.Insert(0, "Select a Resource File");

            //SelectList dropList = new SelectList(resxList);

            //return View(dropList);



            return RedirectToAction("Edit","Language", new { id = x.ToString() });
        }

        public ActionResult Edit(string id)
        {

            string filename = Request.PhysicalApplicationPath +"App_GlobalResources\\" + id;
            Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            ResourceReader resReader = new System.Resources.ResourceReader(stream);
            IDictionaryEnumerator RrEn = resReader.GetEnumerator();
            SortedList slist = new SortedList();
            while (RrEn.MoveNext())
            {
                slist.Add(RrEn.Key, RrEn.Value);
            }
            resReader.Close();
            stream.Dispose();

            //ResourceManager resManager = new ResourceManager(id, System.Reflection.Assembly.GetExecutingAssembly());

            return View(slist);
        }


    }
}
